package com.ruoyi.zhgd.controller;

import java.util.*;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.zhgd.domain.vo.NAsphaltDataResVo;
import com.ruoyi.zhgd.domain.vo.NAsphaltProduceDataVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhgd.domain.vo.NAsphaltDataVo;
import com.ruoyi.zhgd.domain.bo.NAsphaltDataBo;
import com.ruoyi.zhgd.service.INAsphaltDataService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 沥青拌合站数据
 *
 * @author zyb
 * @date 2023-09-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhgd/asphaltData")
public class NAsphaltDataController extends BaseController {

    private final INAsphaltDataService iNAsphaltDataService;

    /**
     * 查询沥青拌合站数据列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<NAsphaltDataVo> list(NAsphaltDataBo bo, PageQuery pageQuery) {
        return iNAsphaltDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出沥青拌合站数据列表
     */
    @SaCheckLogin
    @Log(title = "沥青拌合站数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NAsphaltDataBo bo, HttpServletResponse response) {
        List<NAsphaltDataVo> list = iNAsphaltDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "沥青拌合站数据", NAsphaltDataVo.class, response);
    }

    /**
     * 获取沥青拌合站数据详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<NAsphaltDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iNAsphaltDataService.queryById(id));
    }

    /**
     * 新增沥青拌合站数据
     */
    @SaCheckLogin
    @Log(title = "沥青拌合站数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NAsphaltDataBo bo) {
        return toAjax(iNAsphaltDataService.insertByBo(bo));
    }

    /**
     * 修改沥青拌合站数据
     */
    @SaCheckLogin
    @Log(title = "沥青拌合站数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NAsphaltDataBo bo) {
        return toAjax(iNAsphaltDataService.updateByBo(bo));
    }

    /**
     * 删除沥青拌合站数据
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "沥青拌合站数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iNAsphaltDataService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取最新数据
     */
    @SaCheckLogin
    @GetMapping("/getLatestData")
    public NAsphaltDataVo getLatestData(NAsphaltDataBo bo) {
        return iNAsphaltDataService.getLatestData(bo);
    }

    /**
     * 获取全天生产数据
     */
    @SaCheckLogin
    @GetMapping("/getDayChartData")
    public NAsphaltDataResVo getDayChartData(NAsphaltDataBo bo) {
        return iNAsphaltDataService.getDayChartData(bo);
    }
    /**
     * 获取配方编号列表
     */
    @SaCheckLogin
    @GetMapping("/getInitMatchCode")
    public R<List<Map<String, Object>>> getInitMatchCode() {
        return iNAsphaltDataService.getInitMatchCode();
    }

    /**
     * 油石比检测
     * @return
     */
    @SaCheckLogin
    @GetMapping("/produceAsphaltAggregate")
    public TableDataInfo<NAsphaltProduceDataVo> produceAsphaltAggregate(NAsphaltDataBo bo, PageQuery pageQuery) {
        return iNAsphaltDataService.produceAsphaltAggregate(bo, pageQuery);
    }


    /**
     * 数据采集校验
     * @param id
     * @return
     */
    @SaIgnore
    @GetMapping("/isQualified")
    public R isQualified(@NotNull @RequestParam("id") String id) {
        // 等待数据采集完成后进行判定 等待时间 2分钟
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                iNAsphaltDataService.isQualified(id);
            }
        };
        timer.schedule(timerTask, 2 * 60 * 1000);
        return R.ok("", id);
    }

    /**
     * 手动校验
     * @param bo
     * @return
     */
    @SaCheckLogin
    @PostMapping("/handCheck")
    public R handCheck(@Validated(EditGroup.class) @RequestBody NAsphaltDataBo bo) {
        iNAsphaltDataService.handCheck(bo);
        return R.ok();
    }

}
